package gui.event;

import dto.ImageEvent;
import gui.media.PictureWindow;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ResourceBundle;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import map.MapManager;
import org.jdesktop.swingx.mapviewer.GeoPosition;

/**
 * Panel that represents an image event
 * 
 * @author Pascal
 */
public class ImageEventPanel extends AbstractEventPanel implements Runnable {

    private JLabel picLabel;
    private ImageEvent event;
    
    /**
     * Creates a new ImageEventPanel with an image associated
     * 
     * @param image the image to set to the content
     * @throws NullPointerException if image is null
     */
    public ImageEventPanel(ImageEvent event) {
        
        
        
        if (event == null) {
            this.picLabel.setText(ResourceBundle.getBundle("gui/guiLabels").getString("imagenotavailable"));
        }
        else {
            this.event = event;
            
            picLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    try {
                        if (evt.getClickCount() == 2)
                            new PictureWindow(ImageEventPanel.this.event.getEventName(), ImageEventPanel.this.event.getImage().getPicture()).setVisible(true);
                    }
                    catch (Exception e) {
                    }
                }
            });

            new Thread(this).start(); //check whether the image needs resizing and resize it (maybe time consuming)
            
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
            
            labelTime.setText(formatter.format(event.getEventDate().getTime()));
            labelName.setText(event.getEventName());
            paintEventOnMap();
            
            
            
        }
    }
    
    
    
    @Override
    public JComponent getContentComponent() {
        picLabel = new JLabel();
        
        //this method is called before the constructor -> set image in constructor
        
        return picLabel;
    }

    @Override
    public String getContentLabelText() {
        return ResourceBundle.getBundle("gui/guilabels").getString("picture");
    }

    @Override
    public void run() {
        try {
            final Image image = ImageIO.read(new File(event.getImage().getPath()));

            int tmpWidth = image.getWidth(null);

            while (tmpWidth == -1) { //maybe the pic is not loaded directly therefore try to reload until its done
                tmpWidth = image.getWidth(null);
            }

            final int width = tmpWidth;

            int height = image.getHeight(null);

            float ratio;
            //check whether the image needs resizing
            if (width > 700 || height > 700) {
                //compute the ratio depending on the image is portrait or landscape format
                if (width > height) {
                    ratio = 700.0f / (float)width;
                }
                else {
                    ratio = 700.0f / (float)height;
                }

                final int newWidth = (int)(width * ratio);
                int newHeight = (int)(height * ratio);

                final Image scaledImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);

                //Set the image via UI Thread
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        picLabel.setIcon(new ImageIcon(scaledImage));
                        splitPane.setDividerLocation(newWidth + 50);
                    }
                });
            }
            else {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        picLabel.setIcon(new ImageIcon(image));
                        splitPane.setDividerLocation(width + 50);
                    }
                });
            }
        }
        catch (IOException e) {
            SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        picLabel.setText(ResourceBundle.getBundle("gui/guiLabels").getString("imagenotavailable"));
                    }
                });
        }
    }

    @Override
    protected void paintEventOnMap() {
    
        MapManager mapManager = new MapManager(map);
        
        GeoPosition geoPos = new GeoPosition(event.getPos().getLanDeg(), event.getPos().getLonDeg());
        mapManager.drawTrack(event.getTrack().getTrackList());
        mapManager.paintGPSPoint(geoPos);
    }
    
    
    
    
    
}
